package com.yan.Controller;


import com.yan.Dao.BookMapper;
import com.yan.Pojo.Books;
import com.yan.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;


    @RequestMapping("/allBook")
    public String allBook(Model model) {
        List<Books> list = bookService.queryAllBook();

        model.addAttribute("list", list);
        return "allBook";

    }

    @RequestMapping("/toaddBook")
    public String toaddBook() {
        return "addBook";

    }


    @RequestMapping("/addBook")
    public String addBook(Books books) {
        System.out.println("Books=>" + books);
        bookService.addBooks(books);
        return "redirect:/book/allBook";

    }


    @RequestMapping("/toupdateBook")
    public String toupdateBook(int id, Model model) {
        System.out.println("bookID=>" + id);
        Books books = bookService.queryBookById(id);
        System.out.println("books=>" + books);
        model.addAttribute("QBook", books);
        return "updateBook";

    }


    @RequestMapping("/updateBook")
    public String updateBook(Books books) {
        System.out.println("books==>" + books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";

    }


    @RequestMapping("/delectBook/{bookID}")
    public String delectBook(@PathVariable("bookID") int id) {

        bookService.delectBookById(id);
        return "redirect:/book/allBook";

    }


    @RequestMapping("/queryBookName")
    public String queryBookName(String queryBookName, Model model) {
        System.out.println("BookName==>"+queryBookName);
        Books books = bookService.queryBookName(queryBookName);
        List<Books> list = new ArrayList<>();
        list.add(books);
        if (books==null){
           list=bookService.queryAllBook();
            model.addAttribute("error","未到相关的数据");
        }
        model.addAttribute("list",list);
        return "allBook";

    }

}
